home *** CD-ROM | disk | FTP | other *** search
/ Game Programming in C++ - Start to Finish / GameProgrammingS.iso / Peon / PeonSDK-Win32-1.0.0.exe / {app} / PeonMain / source / FileLogger.cpp < prev    next >
C/C++ Source or Header  |  2005-08-19  |  3KB  |  141 lines

  1. /*
  2. PEON - Win32 Games Programming Library
  3. Copyright (C) 2002-2005 Erik Yuzwa
  4.  
  5. This library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Library General Public
  7. License as published by the Free Software Foundation; either
  8. version 2 of the License, or (at your option) any later version.
  9.  
  10. This library is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13. Library General Public License for more details.
  14.  
  15. You should have received a copy of the GNU Library General Public
  16. License along with this library; if not, write to the Free
  17. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  18.  
  19. Erik Yuzwa
  20. book AT wazooinc DOT com
  21. */
  22.  
  23. #include "FileLogger.h"
  24.  
  25. namespace peon
  26. {
  27.     template<> FileLogger* ISingleton<FileLogger>::ms_Singleton = 0;
  28.     
  29.     FileLogger* FileLogger::getSingletonPtr(void)
  30.     {
  31.         return ms_Singleton;
  32.     }
  33.  
  34.     FileLogger& FileLogger::getSingleton(void)
  35.     {  
  36.         assert( ms_Singleton );  
  37.         return ( *ms_Singleton ); 
  38.         
  39.     }
  40.  
  41.     
  42.     FileLogger::FileLogger( int flags ) : m_logging_level(flags)
  43.     {
  44.  
  45.     }
  46.  
  47.     FileLogger::~FileLogger()
  48.     {
  49.         closeLogStream();
  50.     }
  51.  
  52.     bool FileLogger::openLogStream(const String& strName)
  53.     {
  54.         m_log_file.open( strName.c_str() );
  55.         m_strLogName = strName;
  56.  
  57.         logInfo("FileLogger", "************ begin logfile **********");
  58.         
  59.         return true;
  60.     }
  61.  
  62.     void FileLogger::closeLogStream()
  63.     {
  64.  
  65.         logInfo("FileLogger", "************ end logfile **********");
  66.     
  67.         m_log_file.close();
  68.         
  69.  
  70.     }
  71.  
  72.     void FileLogger::logDebug(const String& strObject, const String& strText)
  73.     {
  74.         String strLogString;
  75.         TCHAR strOutput[MAX_PATH];
  76.  
  77.     
  78.         sprintf(strOutput, "[%s]| DEBUG | %s\n", strObject.c_str(), strText.c_str() );
  79.         strLogString = strOutput;
  80.     
  81.         
  82.         writeToLogStream( strLogString );
  83.  
  84.     }
  85.  
  86.     void FileLogger::logError(const String& strObject, const String& strText)
  87.     {
  88.         String strLogString;
  89.         TCHAR strOutput[MAX_PATH];
  90.  
  91.         sprintf(strOutput, "[%s] | ERROR | %s\n", strObject.c_str(), strText.c_str() );
  92.         strLogString = strOutput;
  93.     
  94.         
  95.         writeToLogStream( strLogString );
  96.     }
  97.  
  98.     void FileLogger::logInfo( const String& strObject, const String& strText)
  99.     {
  100.         String strLogString;
  101.         TCHAR strOutput[MAX_PATH];
  102.  
  103.         sprintf(strOutput, "[%s] | INFO | %s\n", strObject.c_str(), strText.c_str() );
  104.         strLogString = strOutput;
  105.     
  106.         
  107.         writeToLogStream( strLogString );
  108.  
  109.     }
  110.  
  111.     void FileLogger::logFatal( const String& strObject, const String& strText)
  112.     {
  113.  
  114.         String strLogString;
  115.         TCHAR strOutput[MAX_PATH];
  116.  
  117.     
  118.         sprintf(strOutput, "[%s] | FATAL | %s\n", strObject.c_str(), strText.c_str() );
  119.         strLogString = strOutput;
  120.     
  121.         
  122.         writeToLogStream( strLogString );
  123.  
  124.     }
  125.  
  126.     void FileLogger::writeToLogStream(const String& strText)
  127.     {
  128.  
  129.         // Write time into log
  130.         struct tm *pTime;
  131.         time_t ctTime; time(&ctTime);
  132.         pTime = localtime( &ctTime );
  133.         m_log_file << std::setw(2) << std::setfill('0') << pTime->tm_hour
  134.             << ":" << std::setw(2) << std::setfill('0') << pTime->tm_min
  135.             << ":" << std::setw(2) << std::setfill('0') << pTime->tm_sec << ": " << strText << std::endl;
  136.  
  137.         // Flush stcmdream to ensure it is written (incase of a crash, we need log to be up to date)
  138.         m_log_file.flush();
  139.         
  140.     }
  141. }